<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use app\admin\model\ScoreModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class ScoreController extends AdminBaseController
{
//    public function index()
//    {
//        if (!empty($content)) {
//            return $content;
//        }
//
//        $scorePostModel = new ScoreModel();
//        $score         = $scorePostModel->where( 0)->select();
//        $this->assign('score', $score);
//        return $this->fetch();
//    }
    public function index()
    {


        /**搜索条件**/
        $name = $this->request->param('score');

        $score = db('score')

            ->where(function (Query $query) use ($name) {
                if ($name) {
                    $query->where('name', 'like', "%$name%");
                }
            })
            ->order("id DESC")
            ->paginate('3',false);

        // 获取分页显示
        $page = $score->render();
        //

        $this->assign("page", $page);
        $this->assign("score", $score);
        return $this->fetch('',['score'=>$score,'page'=>$page]);
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        $data           = $this->request->param();
        $scorePostModel = new ScoreModel();
        $result         = $this->validate($data, 'Slide');
        if ($result !== true) {
            $this->error($result);
        }
        $scorePostModel->save($data);

        $this->success("添加成功！", url("score/index"));
    }

    public function edit()
    {
        $id             = $this->request->param('id');
        $scorePostModel = new ScoreModel();
        $result         = $scorePostModel->where('id', $id)->find();
        $this->assign('result', $result);
        return $this->fetch();
    }

    public function editPost()
    {
        $data           = $this->request->param();
        $scorePostModel = new ScoreModel();
        $result         = $this->validate($data, 'Slide');
        if ($result !== true) {
            $this->error($result);
        }
        $scorePostModel->save($data, ['id' => $data['id']]);
        $this->success("保存成功！", url("score/index"));
    }

    public function delete($id)
    {
        //根据主键id进行删除
        $del=ScoreModel::destroy($id);
        //判断是否删除成功
        if($del){
            $this->success('删除成功',url('score/index'));
        }else{
            $this->error('删除失败');
        }
    }
}